/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.planning.tactical.wizard.spray;

import java.awt.Component;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JTable;
import junit.framework.Assert;
import nl.cloudfarming.client.model.CropProtection;
import nl.cloudfarming.client.planning.tactical.TacticalPlanningModule;
import org.junit.Before;
import org.junit.Test;
import org.openide.util.NbBundle;
import org.uispec4j.Panel;
import org.uispec4j.UISpec4J;
import org.uispec4j.finder.ComponentMatcher;

/**
 *
 * @author Merijn Zengers
 */
public class CropProtectionListPanelIT {

    private ArrayList<CropProtection> cpList = new ArrayList<CropProtection>();
    private final TableComponentMatcher tableMatcher = new TableComponentMatcher();
    private Date date = new Date();
    private final String DATE_FORMAT = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.spray.crop_protection_list_panel.date_format");

    static {
        UISpec4J.init();
    }

    @Before
    public void setUpClass() {
        CropProtection.Builder cropProtectionDummy1 = new CropProtection.Builder();
        cropProtectionDummy1.setName("Dummy_Roundup");
        cropProtectionDummy1.setLastUsage(date);
        CropProtection cropProtection1 = cropProtectionDummy1.build();

        CropProtection.Builder cropProtectionDummy2 = new CropProtection.Builder();
        cropProtectionDummy2.setName("Dummy_Starane");
        cropProtectionDummy2.setLastUsage(date);
        CropProtection cropProtection2 = cropProtectionDummy2.build();


        cpList.add(cropProtection1);
        cpList.add(cropProtection2);
    }

    /**
     * Test of getSelectedCropProtections method, of class CropProtectionPanel.
     * Tests if the panel contains the right data ans if the method returns the right selected data
     */
    @Test
    public void testGetAndSetSelectedCropProtections() {
        CropProtectionPanel visualPanel = new CropProtectionPanel();
        visualPanel.setSelectedCropProtections(cpList);
        Panel panel = new Panel(visualPanel);

        Assert.assertTrue(panel.containsComponent(tableMatcher).isTrue());

        Assert.assertNotNull(panel.getSwingComponents(tableMatcher));
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        String formattedStartDate = sdf.format(date);

        Assert.assertTrue(panel.getTable().rowEquals(0, new String[]{"Dummy_Roundup", formattedStartDate}).isTrue());

        panel.getTable().addRowToSelection(0);

        Assert.assertTrue(visualPanel.getSelectedCropProtections()[0][0].equals("Dummy_Roundup"));
        Assert.assertTrue(visualPanel.getSelectedCropProtections()[0][1].equals(formattedStartDate));
    }

    @Test
    public void getNameTest() {
        CropProtectionPanel visualPanel = new CropProtectionPanel();

        Assert.assertEquals(NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.spray.crop_protection_list_panel.title"), visualPanel.getName());
    }

    class TableComponentMatcher implements ComponentMatcher {

        @Override
        public boolean matches(Component component) {
            return (component instanceof JTable);
        }
    }
}
